import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:furniture_demo/models/Product.dart';

Future<List<Product>> fetchProducts() async {
  const String apiUrl =
      "https://5f210aa9daa42f001666535e.mockapi.io/api/products";

  final response = await Dio()
      .get(apiUrl, options: Options(responseType: ResponseType.plain));
  if (response.statusCode == 200) {
    List<Product> productList = (json.decode(response.toString()) as List)
        .map((data) => Product.fromJson(data))
        .toList();

    return productList;
  } else {
    throw Exception('fail to load');
  }
}
